
package com.situ.ssm.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.ssm.entity.Banji;
import com.situ.ssm.entity.Course;
import com.situ.ssm.entity.PageBean;
import com.situ.ssm.service.IBanjiService;
import com.situ.ssm.service.ICourseService;
import com.situ.ssm.vo.BanjiSearchCondition;

@Controller
@RequestMapping(value="/banji")
public class BanjiController {
	@Autowired
	private IBanjiService banjiService;
	@Autowired
	private ICourseService courseService;
	
	@RequestMapping(value="list")
	@ResponseBody
	public List<Banji> list() {
		return banjiService.list();
	}
	//分页
	@RequestMapping(value="/pageList")
	public String pageList(BanjiSearchCondition condition,Model model) {
		if (condition.getPageNo() == null) {
			condition.setPageNo(1);
		}
		if (condition.getPageSize() == null) {
			condition.setPageSize(3);
		}
		
		PageBean<Map<String, Object>> pageBean = banjiService.getPageBean(condition);
		model.addAttribute("pageBean",pageBean);
		System.out.println(pageBean);
		model.addAttribute("condition",condition);
		return "banji/banji_list";
	}
	//添加展示课程信息
	@RequestMapping(value="/getInsertPage")
	public String getInsertPage(Model model) {
		List<Course> list = courseService.list();
		model.addAttribute("list",list);
		return "banji/banji_insert";
	}
	//添加
	@RequestMapping(value="/insert")
	@ResponseBody
	public Boolean insert(String name,Integer people,String teatch,Integer courseId) {
		Banji banji  = new Banji(name, people, teatch, courseId);
		boolean result = banjiService.insert(banji);
		return result;
	}
	//根据id删除
	@RequestMapping(value="/deleteById")
	@ResponseBody
	public Boolean deleteById(Integer id) {
		boolean result = banjiService.deleteById(id);
		return result;
	}
	//批量删除
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Boolean deleteAll(String[] selectIds) {
		boolean result = banjiService.deleteAll(selectIds);
		return result;
	}
	//修改前查询
	@RequestMapping(value="/toUpdate")
	public String toUpdate(Integer id,Model model) {
		Banji banji = banjiService.findById(id);
		model.addAttribute("banji",banji);
		
		List<Course> list = courseService.list();
		model.addAttribute("list",list);
		return "banji/banji_update";
	}
	//修改
	@RequestMapping(value="/update")
	@ResponseBody
	public Boolean update(Integer id,String name,Integer people,String teatch,Integer courseId) {
		Banji banji = new Banji(id, name, people, teatch, courseId);
		boolean result = banjiService.update(banji);
		return result;
	}
	//ajax判断是否重名
	@RequestMapping(value="/checkName")
	public void checkName(String name,HttpServletRequest request,HttpServletResponse response) throws IOException {
		boolean isExist = banjiService.checkName(name);
		response.setContentType("text/html;charset = utf-8");
		response.getWriter().write("{\"isExist\": "+isExist+"}");
	}
}
